
package finalproject.client;

import java.util.List;

import finalproject.shared.Student;
import finalproject.shared.Persoon;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;


public class StudentGui {
    
    private static final String LISTING_ROOT_PANEL = "guiListing";
    private static final String FORM_ROOT_PANEL = "guiForm";
    private static final String TOOL_BAR_ROOT_PANEL = "guiToolBar";
    private static final int EDIT_LINK = 3;
    private static final int REMOVE_LINK = 4;

  //widgets
    protected Button addButton;
    protected Button updateButton;
    protected Button addNewButton;
    protected TextBox naamField;
    protected TextBox emailField;
    protected TextBox richtingField;
    protected TextBox klasField;
    protected Grid listGrid;
    protected Grid formGrid;
    
 
    private List<Student> studenten;
    private Student currentStudent;
    private Persoon currentPersoon;
    protected StudentClass studentService;
        
    
    public void go() {
        addButton = new Button("Add student");
        addNewButton = new Button("Add new student");
        updateButton = new Button("gegevens student");
        naamField = new TextBox();
        emailField = new TextBox();
        richtingField = new TextBox();
        klasField = new TextBox();
        listGrid = new Grid(2,6);

        buildForm();
        placeWidgets();
    }
    
 //events
    public void wireEvents() {
    	listGrid.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
                 Cell cellForEvent = listGrid.getCellForEvent(event);
                 gui_eventListGridClicked(cellForEvent);                
            }});
        
    	addButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventAddButtonClicked();
            }});

    	updateButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventUpdateButtonClicked();
            }});
        
    	addNewButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
            	gui_eventAddNewButtonClicked();
           }});
    }
//opbouw
    private void buildForm() {
        formGrid = new Grid(7,5);
        formGrid.setVisible(false);
        
        formGrid.setWidget(0, 0, new Label("Naam"));
        formGrid.setWidget(0, 1, naamField);

        formGrid.setWidget(1, 0, new Label("Email"));
        formGrid.setWidget(1, 1, emailField);
        
        formGrid.setWidget(2, 0, new Label("Richting"));
        formGrid.setWidget(2, 1, richtingField);

        formGrid.setWidget(3, 0, new Label("Klas"));
        formGrid.setWidget(3, 1, klasField);
        
        formGrid.setWidget(4, 0, updateButton);
        formGrid.setWidget(4, 1, addButton);
        
    }

    // plaatsen
    private void placeWidgets() {
        RootPanel.get(LISTING_ROOT_PANEL).add(listGrid);
        RootPanel.get(FORM_ROOT_PANEL).add(formGrid);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(addNewButton);
    }

    //verbergen 
    public void hideGui() {
    	formGrid.setVisible(false);
    	listGrid.setVisible(false);
    	addNewButton.setVisible(false);
    }

    
  //laden student gegevens
    private void loadForm(Student student) {
        this.formGrid.setVisible(true);
        currentStudent = student;
        currentPersoon = student.getPersoon();
        this.naamField.setText(currentPersoon.getNaam());
        this.emailField.setText(student.getEmail());
        this.richtingField.setText(student.getRichting());
        this.klasField.setText(student.getKlas());
    }


  //ophalen student gegevens
    private void copyFieldDateToStudent() {
        currentPersoon.setNaam(naamField.getText());
        currentStudent.setEmail(emailField.getText());
        currentStudent.setPersoon(currentPersoon);
        currentStudent.setRichting(richtingField.getText());
        currentStudent.setKlas(klasField.getText());
    }

 //buttons
    public void gui_eventListGridClicked(Cell cellClicked) {
         int row = cellClicked.getRowIndex();
         int col = cellClicked.getCellIndex();
         Student student = this.studenten.get(row);
     
         if (col==EDIT_LINK) {
             this.addNewButton.setVisible(false);
             this.updateButton.setVisible(true);
             this.addButton.setVisible(false);
             loadForm(student);
         } else if (col==REMOVE_LINK) {
        	 if (Window.confirm("Verwijderen student ?")) {
        		 this.studentService.removeStudent(student);
        	 }
         }
    }


   // add button clicked.
     
    public void gui_eventAddButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToStudent();
        this.studentService.addStudent(currentStudent);
    }

    
     //  update button clicked.
     
    public void gui_eventUpdateButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToStudent();
        this.studentService.updateStudent(currentStudent);
    }

   // add new button clicked.
     
    public void gui_eventAddNewButtonClicked() {
        this.addNewButton.setVisible(false);
        this.updateButton.setVisible(false);
        this.addButton.setVisible(true);
        Student student = new Student();
        Persoon persoon = new Persoon();
        student.setPersoon(persoon);
        loadForm(student);
    }


    
     // Service_event list retrieved from service.
 
    public void service_eventListRetrievedFromService(List<Student> result) {
      

       
    	listGrid.setVisible(true);
    	addNewButton.setVisible(true);
        
        this.studenten = result;
        this.listGrid.clear();
        this.listGrid.resizeRows(this.studenten.size());
        int row = 0;
        this.addNewButton.setVisible(true);
        
        for (Student student : result) {
        	Persoon persoon = student.getPersoon();
        	if (persoon == null) {
        		persoon = new Persoon("--");
        	
        	}
        	this.listGrid.setWidget(row, 0, new Label (persoon.getNaam()));
            this.listGrid.setWidget(row, 1, new Label (student.getEmail()));
            this.listGrid.setWidget(row, 2, new Label (student.getRichting()));
            this.listGrid.setWidget(row, 3, new Label (student.getKlas()));
            this.listGrid.setWidget(row, EDIT_LINK, new Anchor("Edit"));
            this.listGrid.setWidget(row, REMOVE_LINK, new Anchor("Remove"));
            row ++;
        }
    }

    // add student successful.
     
    public void service_eventAddStudentSuccessful() {
        this.studentService.listStudenten();
    }

 // update successful.
     
    public void service_eventUpdateSuccessful() {
        this.studentService.listStudenten();
    }
    
    //remove student successful.
     
    public void service_eventRemoveStudentSuccessful() {
        this.studentService.listStudenten();
    }

   // update student failed.
    public void service_eventUpdateStudentFailed(Throwable caught) {
    	Window.alert("Update student failed");
    }

//add student failed
    public void service_eventAddStudentFailed(Throwable caught) {
        Window.alert("Add student failed");
    }

  //remove student failed
    public void service_eventRemoveStudentFailed(Throwable caught) {
    	Window.alert("Remove student failed");  
    }

 //list student failed 
    public void service_eventListStudentenFailed(Throwable caught) {
    	Window.alert("Unable to get student list");
        
    }

}

